package org.i1510.common.utils.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.i1510.common.utils.ConfPropertiesUtils;

public class FtpUtils {

	public static boolean uploadFile(String filePath,String fileName,String fileDirectory){
		FTPClient ftpClient = new FTPClient(); 
        FileInputStream fis = null; 

        try { 
        	String path = "/com/landray/kmss/joeone/sr/conf.properties";
        	ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
        	Properties pro = pUtils.loadPro(path);
        	
            ftpClient.connect(pro.getProperty("srm.ftp.address")); 
            ftpClient.login(pro.getProperty("srm.ftp.user"),pro.getProperty( "srm.ftp.password")); 

			/*******上传文件*******/
            File srcFile = new File(filePath); 
            fis = new FileInputStream(srcFile); 
            //设置上传目录 
            ftpClient.makeDirectory(pro.getProperty("srm.ftp.file.path")+"/"+fileDirectory);
            ftpClient.changeWorkingDirectory(pro.getProperty("srm.ftp.file.path")+"/"+fileDirectory);
            ftpClient.setBufferSize(1024); 
            ftpClient.setControlEncoding("GBK"); 
            //设置文件类型（二进制） 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
            ftpClient.storeFile(fileName, fis); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错！", e); 
            
        } finally { 
            IOUtils.closeQuietly(fis); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常！", e); 
            } 
        } 
        return true;
	}
	public static boolean uploadFile(){
		FTPClient ftpClient = new FTPClient(); 
        FileInputStream fis = null; 

        try { 
        	String path = "/com/landray/kmss/joeone/sr/conf.properties";
        	ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
        	Properties pro = pUtils.loadPro(path);
        	
            ftpClient.connect(pro.getProperty("srm.ftp.address")); 
            ftpClient.login(pro.getProperty("srm.ftp.user"),pro.getProperty( "srm.ftp.password")); 

            File srcFile = new File("C:\\test.txt"); 
            fis = new FileInputStream(srcFile); 
            //设置上传目录 
            ftpClient.changeWorkingDirectory(pro.getProperty("srm.ftp.file.path")); 
            ftpClient.setBufferSize(1024); 
            ftpClient.setControlEncoding("GBK"); 
            //设置文件类型（二进制） 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
            ftpClient.storeFile("ftp_test1.txt", fis); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错！", e); 
            
        } finally { 
            IOUtils.closeQuietly(fis); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常！", e); 
            } 
        } 
        return true;
	}
	public static void main(String[] args) {
		FTPClient ftpClient = new FTPClient(); 
        FileInputStream fis = null; 

        try { 
        	String path = "/com/landray/kmss/joeone/sr/conf.properties";
        	ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
        	Properties pro = pUtils.loadPro(path);
        	
            ftpClient.connect(pro.getProperty("srm.ftp.address")); 
            ftpClient.login(pro.getProperty("srm.ftp.user"),pro.getProperty( "srm.ftp.password")); 

			/*******上传文件*******/
            FTPFile[] ffs = ftpClient.listFiles(pro.getProperty("srm.ftp.file.path"));       
            for (int i = 0; i < ffs.length; i++) {
				FTPFile file = ffs[i];
				System.out.println(file.getName());
			}
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错！", e); 
            
        } finally { 
            IOUtils.closeQuietly(fis); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常！", e); 
            } 
        } 
  
	}
	/**
	*@author wuhengbo@joeone.net
	*功能:
	*@return 
	* @version $Id: FtpUtils.java,v 1.1 2015-3-26 上午09:51:00  Exp $
	*/
	public static List<String> getAudioFileList(){
		FTPClient ftpClient = new FTPClient(); 
		List<String> list = new ArrayList<String>();

        try { 
        	String path = "/com/landray/kmss/joeone/shopmng/conf.properties";
        	ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
        	Properties pro = pUtils.loadPro(path);
        	
            ftpClient.connect(pro.getProperty("media.ftp.address")); 
            ftpClient.login(pro.getProperty("media.ftp.user"),pro.getProperty( "media.ftp.password")); 

			/*******上传文件*******/
            FTPFile[] ffs = ftpClient.listFiles(pro.getProperty("media.ftp.file.path"));       
            for (int i = 0; i < ffs.length; i++) {
				FTPFile file = ffs[i];
				list.add(file.getName());
				System.out.println(file.getName());
			}
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错！", e); 
            
        } finally { 
 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常！", e); 
            } 
        } 
        return list;
	}
}
